package cn.yx.common.core.util.http;

import cn.yx.common.core.constant.UserAgentConstant;
import cn.yx.common.core.enums.client.ClientAppEnum;
import cn.yx.common.core.enums.client.ClientOsEnum;

/**
 * <p>UserAgent工具类，用于获取访问客户端的信息</p>
 *
 * @author Wgssmart
 */
public final class UserAgentUtil {

    private UserAgentUtil() {
    }

    /**
     * 根据userAgent字符串获取访问客户端的名称，如iOS，Android，Windows，Mac
     *
     * @param userAgent
     * @return
     */
    public static String getClientOsName(String userAgent) {
        if (userAgent.contains(UserAgentConstant.WINDOWS)) {
            return ClientOsEnum.WINDOWS.getValue();
        }
        if (userAgent.contains(UserAgentConstant.MAC)) {
            return ClientOsEnum.MAC.getValue();
        }
        if (userAgent.contains(UserAgentConstant.IPHONE)
                || userAgent.contains(UserAgentConstant.IPAD)
                || userAgent.contains(UserAgentConstant.IPOD)) {
            return ClientOsEnum.IOS.getValue();
        }
        if (userAgent.contains(UserAgentConstant.ANDROID)) {
            return ClientOsEnum.ANDROID.getValue();
        }
        if (userAgent.contains(UserAgentConstant.LINUX)) {
            return ClientOsEnum.LINUX.getValue();
        }
        return ClientOsEnum.OTHER.getValue();
    }

    /**
     * 根据userAgent字符串获取访问客户端的App名称，如微信和QQ
     *
     * @param userAgent
     * @return
     */
    public static String getClientAppName(String userAgent) {
        if (userAgent.contains(UserAgentConstant.WECHAT)) {
            return ClientAppEnum.WECHAT.getDes();
        }
        if (userAgent.contains(UserAgentConstant.QQ)) {
            return ClientAppEnum.QQ.getDes();
        }
        if (userAgent.contains(UserAgentConstant.ALIPAY)) {
            return ClientAppEnum.ALIPAY.getDes();
        }
        return ClientAppEnum.OTHER.getDes();
    }

}
